let id = 0;

class Dep {
  // 每个属性， 都分配一个dep， dep可以存放watcher， watcher中还要存放这个dep
  constructor() {
    this.id = id++;

    this.subs = []; // 用来存放watcher
  }
  depend() {
    // Dep.target dep里要存放这个watcher， watcher中需要存放dep， 多对多的关系
    if (Dep.target) {
      // console.log("eeedepend");
      Dep.target.addDep(this);
    }
  }

  addSub(watcher) {
    this.subs.push(watcher);
  }

  notify() {
    this.subs.forEach((watcher) => watcher.update());
    // console.log("notify()", this.subs);
  }
}

Dep.target = null;

let stack = [];

export function pushTarget(watcher) {
  Dep.target = watcher;
  stack.push(watcher);
}

export function popTarget() {
  // Dep.target = null;
  stack.pop();
  Dep.target = stack[stack.length - 1];
}

export default Dep;
